from django.http import JsonResponse
from rest_framework.views import APIView
import pandas as pd
from .models import st_data
from myproject.settings import MEDIA_ROOT


class tu_biao(APIView):
#     def get(self, request):
#         newmounth = int(request.query_params.get('newmounth'))
# #       1. 读取 Excel 文件
#         excel_file = MEDIA_ROOT + "\测试数据.xlsx"
#         # print(excel_file)
#         df = pd.read_excel(excel_file)
#
#         # 2. 选择特定列
#         time = df['Date']
#         selected_columns = df['Active Users']  # 替换为你的列名
#         selected_columns2 = df['Active Users (Change Value)']
#
#         # # 3. 插入列名到首行
#         time = pd.concat([pd.Series(['时间']), time], ignore_index=True)
#         selected_columns = pd.concat([pd.Series(['上周收入']), selected_columns],
#                                      ignore_index=True)
#         selected_columns2 = pd.concat([pd.Series(['收入变化']), selected_columns2],
#                                      ignore_index=True)
#         # 转换为列表
#         time = time.values.tolist()
#         selected_columns = selected_columns.values.tolist()
#         selected_columns2 = selected_columns2.values.tolist()
#         a = (newmounth - 1) * 4
#         b = newmounth * 5
#         if newmounth == 0:
#             data = {
#                 "all": [time, selected_columns, selected_columns2]
#             }
#         else:
#             data = {
#                 "all" : [time[a:b],selected_columns[a:b],selected_columns2[a:b]]
#             }
#         return JsonResponse(data)
    def get(self, request):
        data = st_data.objects.using('data').filter(App='大话西游').values('Date', 'Downloads')
        data = list(data)
        le = len(data)
        resault = []
        for i in range(le):
            data[i]['Date'] = data[i]['Date'].strftime('%Y-%m-%d')
            resault.append([data[i]['Date'], data[i]['Downloads']])
        # print(resault)
        return JsonResponse(resault, safe=False)
